<script>
import {doGet, doPost, doPut} from "../http/httpRequest.js";
import {messageTip} from "../util/util.js";

export default {
  name: "ActivityRecordView",

  //注入
  inject: ['reload'],

  data() {
    return {
      //市场活动表单对象
      activityQuery: {},

      //市场活动表单校验规则
      activityRules: {
        ownerId: [
          {required: true, message: "请选择负责人", trigger: "blur"}
        ],
        name: [
          {required: true, message: "请输入活动名称", trigger: "blur"}
        ],
        startTime: [
          {required: true, message: "请选择开始时间", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "请选择结束时间", trigger: "blur"}
        ],
        cost: [
          {required: true, message: "请输入活动预算", trigger: "blur"},
          {pattern: /^[0-9]+(.[0-9]{2})?$/, message: "请输入正确的活动预算", trigger: "blur"}
        ],
        description: [
          {required: true, message: "请输入活动描述", trigger: "blur"},
          {min: 5, max: 255, message: "请输入5-255个字符", trigger: "blur"}
        ]
      },

      //市场活动负责人下拉框数据
      ownerOptions: [
        {}
      ]
    }
  },

  methods: {

    //获取市场活动负责人下拉框数据
    loadOwner() {
      doGet("/api/owner", {}).then(resp => {
        if (resp.data.code == 200) {
          this.ownerOptions = resp.data.data;
        }
      })
    },

    //返回函数
    goBack() {
      this.$router.go(-1)
    },

    //市场活动提交
    activitySubmit() {
      let formData = new FormData();
      for (let field in this.activityQuery) {
        console.log(field + "---" + this.activityQuery[field])
        if (this.activityQuery[field]) {
          formData.append(field, this.activityQuery[field]);
        }
      }
      this.$refs.activityReForm.validate((isValid) => {
        if (isValid) {

          if (this.activityQuery.id > 0) {
            //编辑
            doPut("/api/activity", formData).then(resp => {
              if (resp.data.code == 200) {
                messageTip("编辑市场活动成功", "success")
                //实现页面跳转
                this.$router.push("/dashboard/activity");
              } else {
                messageTip("编辑市场活动失败", "error")
              }
            })
          } else {
            //新增
            doPost("/api/activity", formData).then(resp => {
              if (resp.data.code == 200) {
                messageTip("新增市场活动成功", "success")
                //实现页面跳转
                this.$router.push("/dashboard/activity");
              } else {
                messageTip("新增市场活动失败", "error")
              }
            })
          }
        }
      })
    },

    //加载要编辑的数据（由于编辑和录入共用一个页面,需要判断）
    loadActivity() {
      let id = this.$route.params.id;
      if (id) {
        //id存在为编辑
        doGet("/api/activity/" + id, {}).then(resp => {
          if (resp.data.code == 200) {
            this.activityQuery = resp.data.data;
          }
        })
      }
    },
  },

  mounted() {
    this.loadOwner();
    //加载要编辑的数据（由于编辑和录入共用一个页面,需要判断）
    this.loadActivity()
  }

}
</script>

<template>
  <el-form ref="activityReForm" :model="activityQuery" label-width="110px" :rules="activityRules">
    <!--负责人-->
    <el-form-item label="负责人" prop="ownerId">
      <el-select v-model="activityQuery.ownerId" placeholder="请选择" class="width" @click="loadOwner()">
        <el-option
            v-for="item in ownerOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
        />
      </el-select>
    </el-form-item>

    <!--活动名称-->
    <el-form-item label="活动名称" prop="name">
      <el-input v-model="activityQuery.name"/>
    </el-form-item>

    <!--开始时间-->
    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
          v-model="activityQuery.startTime"
          type="date"
          placeholder="请选择创建时间"
          :size="size"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%"
      />
    </el-form-item>

    <!--结束时间-->
    <el-form-item label="结束时间" prop="endTime">
      <el-date-picker
          v-model="activityQuery.endTime"
          type="date"
          placeholder="请选择创建时间"
          :size="size"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%"
      />
    </el-form-item>

    <!--活动预算-->
    <el-form-item label="活动预算" prop="cost">
      <el-input v-model="activityQuery.cost"/>
    </el-form-item>

    <!--活动描述-->
    <el-form-item label="活动描述" prop="description">
      <el-input
          v-model="activityQuery.description"
          :rows="6"
          type="textarea"
          placeholder="请输入活动描述"
      />
    </el-form-item>

    <!--提交和重置按钮-->
    <el-form-item>
      <el-button type="primary" @click="activitySubmit()">提 交</el-button>
      <el-button type="primary" plain @click="goBack()">返 回</el-button>
    </el-form-item>

  </el-form>

</template>

<style scoped>
.width {
  width: 100%;
}
</style>